<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/**
 * Simple validator for business objects validation
 * @package Validator
 */
class Validator {
    
    protected $object;
    protected $checks = array();
    protected $errors = array();  
    
    public function __construct($object) {
    	$this->object = $object;
    }

    /**
     * Ajoute un test à effectuer sur le formulaire
     */
    public function addCheck(ValidatorCheck $check) {
        $fieldName = $check->getFieldName();
    	$value = $this->object->$functionName;
        $check->setValue($value);
        $this->checks[$check->getFieldName()] = $check;
    }

    /**
     * Teste le formulaire et génère les erreurs, interrogeables par
     * la méthode FormValidator::isError ou et FormValidator::getErrorMsg
     */
    public function validate() {
        foreach ($this->checks as $check) {
            if (!$check->isValid($this->object)) {
                $this->addError($check->getFieldName());
            }
        }
    }
    
    public function isNoError() {
        return count($this->errors) == 0;
    } 

    protected function addError($fieldName) {
        $this->errors[$fieldName] = $fieldName;
    }
            
    protected function isError($fieldName) {
        return (isset($this->errors[$fieldName]));
    }
    
    public function getErrors() {
    	return $this->errors;
    }
}

//-- Class ValidatorCheck ------------------------------------------------//
/**
 * Simple validator for business objects validation
 * @package Validator
 */
abstract class ValidatorCheck {
    
    protected $fieldName;
    protected $value;
    
    public function __construct($fieldName) {
        $this->fieldName = $fieldName;
    }

    public function setValue($value) {
    	$this->value = $value;
    }
    
    public function getFieldName() {
        return $this->fieldName;
    }

    abstract public function isValid();
    
    protected function getValue() {
        return $this->value;
    }
}

//-- ValidatorCheck childs -----------------------------------------------//
class CheckEmail extends ValidatorCheck {
    public function isValid() {
        return preg_match('/^[\S]+@[\S]+\.\D{2,4}$/', $this->getValue());
    }
}

class CheckPostalCode extends ValidatorCheck {
    public function isValid() {
        $isNumeric = is_numeric($this->getValue());
        $length = strlen($this->getValue());
        return $isNumeric && ($length == 4 || $length == 5);
    }    
}

class CheckString extends ValidatorCheck {
    public function isValid() {
        $length = strlen($this->getValue());
        $isString = is_string($this->getValue());
        return $isString && $length > 0;
    }
}

class CheckAge extends ValidatorCheck {
    public function isValid() {
        $isNumeric = is_numeric($this->getValue());
        $value = $this->getValue();
        return $isNumeric && $value > 1 && $value < 120;
    }
}

class CheckUnsignedInt extends ValidatorCheck {
    public function isValid() {
        $isInt = is_int($this->getValue());
        $value = $this->getValue();
        return $isInt && $value >= 0;
    }
}

class CheckUnsignedNumeric extends ValidatorCheck {
    public function isValid() {
        $isNumeric = is_numeric($this->getValue());
        $value = $this->getValue();
        return $isNumeric && $value >= 0;
    }
}

class CheckCheckbox extends ValidatorCheck {
    public function isValid() {
        return $this->getValue() != null;
    }
}

/*
Vérifie la validité de la date au format YYYY-MM-DD
Source: http://www.expreg.com/lire-Date-source
*/
class CheckDate extends ValidatorCheck {
    public function isValid() {
        $reg = "/([0-9]{4})-([0-9]{2})-([0-9]{2})/";
        return preg_match($reg, $this->getValue());
    }
}

class CheckDateSlash extends ValidatorCheck {
    public function isValid() {
        $reg = "/^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2})$|^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2}\s([0-1]\d|[2][0-3])\:[0-5]\d\:[0-5]\d)$/";
        return preg_match($reg, $this->getValue());
    }
}

/*The password must contain :
    - at least 1 number, 
    - at least 1 lower case letter, and
    - at least 1 upper case letter.
Source: http://nilangshah.wordpress.com/2007/06/26/password-validation-via-regular-expression/*/
class CheckPassword extends ValidatorCheck {
    public function isValid() {
        $reg = "/^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$/";
        return preg_match($reg, $this->getValue());
    }
}


class CheckPuce extends ValidatorCheck {
    public function isValid() {
        $isNumeric = is_numeric($this->getValue());
        $length = strlen($this->getValue());        
        return $isNumeric && $length == 15;
    }
}

?>